﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ionic.Zip;

namespace MediaMonitor
{
    public partial class downLoadPPT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["pptFilePath"] != null)
            {
                string pptFilePath = Session["pptFilePath"].ToString();
                Response.Clear();
                // no buffering - allows large zip files to download as they are zipped
                Response.BufferOutput = false;
                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "filename=downloadFileName.zip");

                using (ZipFile zip = new ZipFile())
                {
                    if (Session["cat"]!=null)
                    {
                        
                        DataTable cat = (DataTable)Session["cat"];
                        foreach (DataRow dr in cat.Rows)
                        {
                            zip.AddFile(dr["filePath"].ToString(), dr["CatName"].ToString());
                        }
                    }
                    if (Session["ad"]!=null)
                    {
                        DataTable ad = (DataTable) Session["ad"];
                        foreach (DataRow dr in ad.Rows)
                        {
                            zip.AddFile(dr["filePath"].ToString(), dr["CatName"].ToString());
                        }
                    }
                    if (Session["prod"] != null)
                    {
                        DataTable prod =(DataTable)Session["prod"];
                        foreach (DataRow dr in prod.Rows)
                        {
                            zip.AddFile(dr["filePath"].ToString(), dr["CatName"].ToString());
                        }
                    }
                    if (File.Exists(pptFilePath))
                    {
                        zip.AddFile(pptFilePath, "");
                        zip.Save(Response.OutputStream);
                       
                        Response.Flush();
                        File.Delete(pptFilePath);
                        
          
                        Response.End();
                    }
                    this.Response.Redirect(Request.Url.AbsolutePath);
                }
            }
        }
    }
}